package mvc;

import java.util.*;
import java.sql.*;

public class Guestbook {
	
	private ArrayList<GbPost> arr = new ArrayList();
	private int pageCount;
	private int totalpages;
	       
public Guestbook(DBconn db, int pageCount, int numberofposts) throws SQLException
{
	GbPost post;

	this.pageCount=pageCount;

	db.sqlQuery("SELECT count(*) FROM gb").next();
	int rowcount = Integer.parseInt(db.getString("count(*)"));	

	// How many posts per side?
	int count = numberofposts; 

	// Total pages needed
	this.totalpages = (rowcount/count);
		
	// int/int = int .. avrundningsfel
	if(rowcount % count > 0)
		this.totalpages++;	
		
	if(this.totalpages < this.pageCount)
		this.pageCount = 1;

	db.sqlQuery("SELECT username,firstname,lastname,gb_text,email, gb_date,gb_id FROM person,gb where person.username = gb.person order by gb_date desc limit "+(this.pageCount-1)*count+"," +count);

	// Put all the gb-post to show in the guestbook-model
	while(db.next()){	
		post = new GbPost(db.getString("username"),db.getString("gb_text"),db.getString("db_date"),db.getString("gb_id"));
		this.addGbPost(post);
	}
}
       
public void addGbPost(GbPost post) {
	arr.add(post);
}

public Iterator getAllGbPosts(){
	Iterator<GbPost> t = arr.iterator();
	return t;
	}	
	
public int getPageCount(){ return pageCount; }	

public int getTotalpages(){	return totalpages; }

public void setPagenumber(int pageCount){
	this.pageCount = pageCount;
}	

public void setTotalpages(int totalpages){
	this.totalpages = totalpages;
}
	
}